const Koa = require('koa');

const bodyParser = require('koa-bodyparser');

const logger = require('koa-logger')

const controller = require('./controller');

const globalConfig = require('./config/config');

const templating = require('./templating');

const rest = require('./rest');

const app = new Koa();


//判断环境变量
const envConfig = globalConfig[app.env];


app.use(logger());


app.use(async(ctx,next)=>{
    await next();
    if (ctx.body || !ctx.idempotent) return;
    // ctx.redirect('back');
})

let staticFiles = require('./static-files');
app.use(staticFiles('/static/', __dirname + '/static'));

app.use(bodyParser());

//模板引擎
app.use(templating('views',{
    noCache:true,
    watch:true
}));

app.use(rest.restify());

app.use(controller());

app.listen(envConfig.port);

console.info('app started at port '+envConfig.port);
